package com.vitabuybuy.tv.ui.home.coupons

import android.os.Bundle
import com.google.android.material.tabs.TabLayout
import com.google.gson.reflect.TypeToken
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.ext.mmkv

import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityCouponsCenterBinding
import com.vitabuybuy.tv.dto.CouponCenterDto
import com.vitabuybuy.tv.dto.FirstCategoryDto
import com.library.common.toast.XToast
import com.library.common.util.JsonUtil
import com.vitabuybuy.tv.ui.home.coupons.viewmodel.CouponsCenterViewModel
import com.vitabuybuy.tv.ui.mine.coupons.MyCouponsActivity
import com.library.common.util.MMKVContancts
import com.vitabuybuy.tv.ui.auth.LoginActivity
import kotlinx.android.synthetic.main.activity_coupons_center.*
import java.util.ArrayList

class CouponsCenterActivity : BaseActivity<CouponsCenterViewModel, ActivityCouponsCenterBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_coupons_center

    private var categoryId = -1
    private var pageNumber = 1

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.coupons_center))
        mBinding.mViewModel = viewModel

    }

    override fun observeData() {
        val categoryList = JsonUtil.fromJson(
            mmkv.decodeString(MMKVContancts.CATEGORY_LIST, ""),
            object : TypeToken<ArrayList<FirstCategoryDto>>() {}.type
        ) as ArrayList<FirstCategoryDto>
        tlClassification.apply {
            removeAllTabs()
            addTab(
                tlClassification.newTab().setText(getString(R.string.all))
                    .setTag(categoryId)
            )
            for (str in categoryList) {
                addTab(tlClassification.newTab().setText(str.name).setTag(str.id))
            }
            viewModel.getCouponList(pageNumber, categoryId)
        }
    }

    override fun eventHandling() {
        tlClassification.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                pageNumber = 1
                categoryId = tab?.tag as Int
                viewModel.getCouponList(pageNumber, categoryId)
            }

            override fun onTabReselected(p0: TabLayout.Tab?) {
            }

            override fun onTabUnselected(p0: TabLayout.Tab?) {
            }
        })
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                viewModel.getCouponList(pageNumber, categoryId)
            }
            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                viewModel.getCouponList(pageNumber, categoryId)
            }
        })
        tvCheckMine.setOnClickListener {
            if (!mmkv.decodeBool(MMKVContancts.IS_LOGIN_IN, false)) {
                LoginActivity.openActivity(mContext)
                return@setOnClickListener
            }
            MyCouponsActivity.openActivity(mContext)
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                pageNumber = 1
                viewModel.getCouponList(pageNumber, categoryId)
            }
            1 -> {
                refresh.onLoad(msg.arg1)
            }
            2 -> {
                if (!mmkv.decodeBool(MMKVContancts.IS_LOGIN_IN, false)) {
                    LoginActivity.openActivity(mContext)
                    return
                }
                viewModel.saveACoupon((msg.obj as CouponCenterDto).id, msg.arg1)
            }
            3 -> {
                viewModel.couponList[msg.arg1].isReceived = true
                XToast.success(getString(R.string.already_save_coupon))
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, CouponsCenterActivity::class.java)
        }
    }
}
